package com.geekaca.atm;

import java.util.ArrayList;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;


public class ATMSystemRefactorMap {
    final static int LOGIN = 1;
    final static int REGISTER = 2;
    final static int EXIT_ = 3;
    static Scanner sc = new Scanner(System.in);
    static Account account;
    static Map<String,Account> accountMap = AccountSys.ACC_INSTANCE.accountMap;

    /*// 创建AccountList 的单例实例对象
    static AccountList accountListInstance = AccountList.getAccountList();
    // 定义一个静态变量,类型为ArrList<Account> 接收 AccountList 工具类中的静态变量 accountList;
    static ArrayList<Account> accountList = accountListInstance.accountList;*/

//    ArrayList<Account> accountList = AccountSys.ACC_INSTANCE.accounts;
//    ArrayList<Account> accountList = accInstance.accounts;

    public static void main(String[] args) {
        initAccounts();
        mainPage();
    }

    public static void mainPage() {
//        ArrayList<Account> accountList = AccountSys.ACC_INSTANCE.accounts;
//        Map<String,Account> accountMap = AccountSys.ACC_INSTANCE.accountMap;
        while (true) {
            System.out.println("============欢迎您进入到操作界面=============");
            System.out.println("1、登录帐户");
            System.out.println("2、注册帐户");
            System.out.println("3、退出");
            System.out.println("请您选择操作: ");

            // 命令
            int command = sc.nextInt();
            switch (command) {
                case LOGIN:
                    // 登录
                    login();
                    break;
                case REGISTER:
                    // 注册
                    register();
                    break;
                case EXIT_:
                    // 退出
                    System.exit(0);
//                    break;
                default:
                    System.out.println("输入有误! ");
                    break;
            }

        }
    }

    private static void login() {
        System.out.println("============欢迎您进入登录操作=============");
        /**
         * 当用户名密码输入正确,那么继续下一环节: 下一个菜单:
         * 选择 取款,存款....
         * 如果用户名或密码错误: 那么继续提示输入
         */
        boolean isBack = false;
        while (true) {
            System.out.println("请输入卡号:");
            String cardNo = sc.next();
            System.out.println("请输入密码:");
            String passwd = sc.next();


            /**
             * 判断卡号和密码是否正确
             */
            account = findAccount(cardNo);
//            System.out.println(findingAccount);
            if (account.getCardID().equals(cardNo) && account.getPassWord().equals(passwd)) {
                System.out.println("=============登录成功,您可以开始办理您的业务了!=============");
                System.out.println("欢迎进入帐户操作:");
//                isBack = accountHandle(accountList, findingAccount, sc);
                accountHandle();
            } else {
                System.out.println("卡号或密码错误,请重新输入!");
            }
//            if (isBack) {
//                break;
//            }

        }

    }

    // 注册账户
    private static void register() {

//        ArrayList<Account> accountList = AccountSys.ACC_INSTANCE.accounts;
        // 创建用户对象
        Account account = new Account();

        // 生成卡号
        String cardNo = genCardID();
        account.setCardID(cardNo);

        System.out.println("请输入您的用户名");
        String inputUserName = sc.next();
        account.setUsername(inputUserName);

        // 添加密码
        while (true) {

            System.out.println("请输入要设置的密码:");
            String password1 = sc.next();
            System.out.println("请再次输要设置的密码:");
            String password2 = sc.next();

            if (password1.equals(password2)) {
                account.setPassWord(password1);
                System.out.println("密码设置成功 ");
                break;
            } else {
                System.out.println("两次密码输入不一致,请重新设置");
            }
        }
        // 设置限额
        System.out.println("请输入取款限额");
        int quotaMoney = sc.nextInt();
        account.setQuotaMoney(quotaMoney);
        // 用单例实例调用静态方法操作集合的增加
//        accountList.add(account);
        accountMap.put(account.getCardID(),account  );

        System.out.println("用户创建完成");
        System.out.println("您的用户名为: " + account.getUsername() + ", 账号为: " + account.getCardID() + ", 每次取款限额为: " + account.getQuotaMoney());
    }

    // 生成卡号:
    private static String genCardID() {
        Random random = new Random();
        String cardNo = "ICBC";
        boolean isExists = true;
        while (true) {
            for (int i = 0; i < 8; i++) {
                cardNo += random.nextInt(10);
            }

            for (String carnum:accountMap.keySet()){
                if(cardNo.equals(carnum)){
                    System.out.println("卡号重复,重新生成.");
                    isExists = false;
                    break;
                }
            }


            if (isExists) {
                return cardNo;
            }
        }
    }

    // 初始化一个账户
//    private static void initAccounts(ArrayList<Account> accountList) {
    private static void initAccounts() {
        Account account = new Account();
        account.setUsername("zhangsan");
        account.setPassWord("abc123");
        account.setCardID("ICBC987654321");
        account.setQuotaMoney(4000);
        account.setMoney(50000);
//        accountList.add(account);
        accountMap.put(account.getCardID(),account);

        Account account2 = new Account();
        account2.setUsername("goulun");
        account2.setPassWord("abc123");
        account2.setCardID("ICBC29851736");
        account2.setQuotaMoney(4000);
        account2.setMoney(10000);
        accountMap.put(account2.getCardID() ,account2);
    }

    // 查找账户
    private static Account findAccount(String cardNo) {
        Account account = null;

        for (Map.Entry<String,Account> entry:accountMap.entrySet()){
            if(entry.getKey().equals(cardNo)){
                account = entry.getValue();
            }
        }

        return account;
    }

    // 业务操作
    private static void accountHandle() {
        while (true) {
            System.out.println("==================您当前账户详情信息如下======================");
            System.out.println("卡号：" + account.getCardID());
            System.out.println("户主：" + account.getUsername());
            System.out.println("余额: " + account.getMoney());
            System.out.println("单次取现限额: " + account.getQuotaMoney());
            System.out.println("==================欢迎您进入到操作界面========================");
            System.out.println("1、查询余额");
            System.out.println("2、存钱");
            System.out.println("3、取款 ");
            System.out.println("4、转帐 ");
            System.out.println("5、修改密码 ");
            System.out.println("6、退出 ");
            System.out.println("7、注销账户 ");
            System.out.println("请您选择操作: ");
            Operate operate = Operate.QUERY;
            int command = sc.nextInt();
            switch (command) {
                case 1:
                    operate = Operate.QUERY;
                    break;
                case 2:
                    operate = Operate.DEPOSIT;
                    break;
                case 3:
                    operate = Operate.WITHDRAW;
                    break;
                case 4:
                    operate = Operate.TRANSFER;
                    break;
                case 5:
                    operate = Operate.MODIFY_PASSWORD;
                    break;
                case 6:
                    operate = Operate.EXIT_;
                    break;
                case 7:
                    operate = Operate.DESTROY;
                    break;
                default:
                    System.out.println("输入有误! ");
                    break;
            }

            switch (operate) {
                case QUERY: //
                    // 查询余额
                    query();
                    break;
                case DEPOSIT:
                    // 存钱
                    save();
                    break;
                case WITHDRAW:
                    // 取钱
                    withdraw();
                    break;
                case TRANSFER:
                    // 转账
                    transfer(accountMap);
                    break;
                case MODIFY_PASSWORD:
                    // 修改密码
                    modifyPassword();
                    break;
                case EXIT_:
                    // 返回
                    mainPage();
                    return;
                case DESTROY:
                    // 注销账户
                    closeAccount();
                    break;
                default:
                    System.out.println("输入有误! ");
                    break;
            }
        }
    }

    // 查询余额
    private static void query() {

        double blance = account.getMoney();
        System.out.println("您的余额为:" + blance + "元");
    }

    // 存钱操作
    private static void save() {
        System.out.println("请输入的存入金额:");
        double money = sc.nextDouble();
        money += account.getMoney();
        account.setMoney(money);
        System.out.println("钱已存入,你的当前余额为: " + account.getMoney());
    }

    // 取款操作
    private static void withdraw() {
        System.out.println("请输入取款金额:");
        double withdraw = sc.nextDouble();
        if (withdraw >= account.getQuotaMoney()) {
            System.out.println("抱歉,您一次的取款限额为: " + account.getQuotaMoney() + "元");
            return;
        }
        if (withdraw > account.getMoney()) {
            System.out.println("余额不足,请您重新输入金额:");
            return;
        }
        account.setMoney(account.getMoney() - withdraw);
        System.out.println("取款成功,您的当前余额为: " + account.getMoney() + "元");
    }

    // 转账操作
    private static void transfer(Map<String,Account> accountMap) {
        System.out.println("请输入对方账号: ");
        String bCardNo = sc.next();
        Account b = findAccount(bCardNo);
        if (b != null) {
            System.out.println("对方账号为:" + b.getUsername());
            String bName = b.getUsername();

            // 获取对方的姓氏
            String bFamilyName = String.valueOf(bName.charAt(0));
            System.out.println("请输入对方的姓氏: ");
            String bfInput = sc.next();

            if (bfInput.equals(bFamilyName)) {
                System.out.println("输入正确");
                System.out.println("请输入转账金额:");
                int money = sc.nextInt();
                // 余额充足,可以转账
                if (account.getMoney() > money) {
                    account.setMoney(account.getMoney() - money);
                    b.setMoney(b.getMoney() + money);
                    System.out.println("转帐完成, 当前余额为" + account.getMoney());
                } else {
                    System.out.println("转帐金额超出账户余额,请重新操作.");
                }

            } else {
                System.out.println("对方姓氏输入不正确.");
            }

        } else {
            System.out.println("账号不存在,请重新输入");
        }

    }

    // 修改密码
    private static void modifyPassword() {
        while (true) {
            System.out.println("请输入原密码:");
            String origPassword = sc.next();
            if (origPassword.equals(account.getPassWord())) {
                System.out.println("请输入新密码");
                String newPassword1 = sc.next();
                if (!newPassword1.equals(origPassword)) {
                    System.out.println("请再次输入新密码");
                    String newPassword2 = sc.next();
                    if (newPassword1.equals(newPassword2)) {
                        account.setPassWord(newPassword1);
                        System.out.println("新密码设置完成,请使用新密码重新登录");
                        break;
                    } else {
                        System.out.println("两次输入不一致,请重新操作.");
                    }
                } else {
                    System.out.println("新密码不能与原密码重复.");
                }


            } else {
                System.out.println("密码输入错误");
                System.out.println("请重新操作.");
            }
        }


    }

    // 注销帐户
    private static void closeAccount() {
//        ArrayList<Account> accountList = AccountSys.ACC_INSTANCE.accounts;

//        用单例实例调用静态方法操作集合的删除
//        accountList.remove(account);
        accountMap.remove(account.getCardID());
//        System.out.println(accountList.toString());
        System.out.println("账户已注销,请重新登录操作...");
        mainPage();
    }
}

